import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../domain/providers/auth_provider.dart';
import '../backup/backup_settings_screen.dart';

class DashboardScreen extends StatelessWidget {
  const DashboardScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final authProvider = Provider.of<AuthProvider>(context);
    final username = authProvider.currentUser?.username ?? '用户';

    return SingleChildScrollView(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 欢迎信息
          Text(
            '你好，$username',
            style: Theme.of(context).textTheme.headlineMedium,
          ),
          const SizedBox(height: 4),
          Text(
            '欢迎回来',
            style: Theme.of(
              context,
            ).textTheme.bodyLarge?.copyWith(color: Colors.grey),
          ),
          const SizedBox(height: 24),

          // 存储使用概况卡片
          _buildStorageCard(context),
          const SizedBox(height: 24),

          // 快速操作
          _buildQuickActions(context),
          const SizedBox(height: 24),

          // 最近活动
          _buildRecentActivities(context),
        ],
      ),
    );
  }

  // 存储使用概况卡片
  Widget _buildStorageCard(BuildContext context) {
    return Card(
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('存储使用概况', style: Theme.of(context).textTheme.titleMedium),
            const SizedBox(height: 16),

            // 存储进度条
            const LinearProgressIndicator(
              value: 0.4, // 示例值：使用了40%的空间
              backgroundColor: Colors.grey,
              borderRadius: BorderRadius.all(Radius.circular(4)),
            ),
            const SizedBox(height: 8),

            // 使用详情
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [Text('已使用: 400MB'), Text('总容量: 1GB')],
            ),

            const SizedBox(height: 16),

            // 文件类型分布
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buildFileTypeItem(context, Icons.image, '图片', '250MB'),
                _buildFileTypeItem(context, Icons.movie, '视频', '100MB'),
                _buildFileTypeItem(context, Icons.article, '文档', '50MB'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  // 文件类型项
  Widget _buildFileTypeItem(
    BuildContext context,
    IconData icon,
    String label,
    String size,
  ) {
    return Column(
      children: [
        Icon(icon, color: Theme.of(context).primaryColor),
        const SizedBox(height: 4),
        Text(label),
        Text(size, style: const TextStyle(color: Colors.grey, fontSize: 12)),
      ],
    );
  }

  // 快速操作
  Widget _buildQuickActions(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('快速操作', style: Theme.of(context).textTheme.titleMedium),
        const SizedBox(height: 16),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildActionButton(
              context,
              icon: Icons.upload_file,
              label: '上传文件',
              onTap: () {
                // 上传文件操作
                ScaffoldMessenger.of(
                  context,
                ).showSnackBar(const SnackBar(content: Text('上传文件功能即将推出')));
              },
            ),
            _buildActionButton(
              context,
              icon: Icons.create_new_folder,
              label: '新建文件夹',
              onTap: () {
                // 新建文件夹操作
                ScaffoldMessenger.of(
                  context,
                ).showSnackBar(const SnackBar(content: Text('新建文件夹功能即将推出')));
              },
            ),
            _buildActionButton(
              context,
              icon: Icons.backup,
              label: '备份',
              onTap: () {
                // 导航到备份设置页面
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const BackupSettingsScreen(),
                  ),
                );
              },
            ),
          ],
        ),
      ],
    );
  }

  // 操作按钮
  Widget _buildActionButton(
    BuildContext context, {
    required IconData icon,
    required String label,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surfaceVariant,
          borderRadius: BorderRadius.circular(12),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(icon, size: 32),
            const SizedBox(height: 8),
            Text(label),
          ],
        ),
      ),
    );
  }

  // 最近活动
  Widget _buildRecentActivities(BuildContext context) {
    // 示例活动数据
    final activities = [
      {'icon': Icons.upload_file, 'text': '上传了文件 "期末报告.docx"', 'time': '2小时前'},
      {'icon': Icons.folder, 'text': '创建了文件夹 "工作计划"', 'time': '昨天'},
      {'icon': Icons.delete, 'text': '删除了文件 "旧照片.jpg"', 'time': '3天前'},
    ];

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('最近活动', style: Theme.of(context).textTheme.titleMedium),
        const SizedBox(height: 16),
        ...activities.map(
          (activity) => _buildActivityItem(
            context,
            icon: activity['icon'] as IconData,
            text: activity['text'] as String,
            time: activity['time'] as String,
          ),
        ),
      ],
    );
  }

  // 活动项
  Widget _buildActivityItem(
    BuildContext context, {
    required IconData icon,
    required String text,
    required String time,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              color: Theme.of(context).colorScheme.primaryContainer,
              shape: BoxShape.circle,
            ),
            child: Icon(icon, size: 20),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(text),
                Text(
                  time,
                  style: const TextStyle(color: Colors.grey, fontSize: 12),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
